<?php
/**
 * Created by mww
 * User: mww
 */

namespace app\admin\controller;
use think\Db;
use think\Request;

class Question extends Base
{
    /**
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $where = [
                'cpd_id' => ['>',0]
            ];
            if(session("company_id") > 0){
                $where['c_id'] = session("company_id");
            }
            if(isset($user_name) && $user_name != ''){
                $where['u1.user_username|u1.user_phone|cpd_name|cp_name|c_name'] = ['like', '%' . $user_name . '%'];
            }
            if(isset($dq_answer) && $dq_answer != ''){
                $where['dq_answer'] = $dq_answer;
            }
            if(isset($dq_zhuan) && $dq_zhuan != ''){
                $where['dq_zhuan'] = $dq_zhuan;
            }
            //开始时间查询
            if(isset($start) && $start != "" && isset($end) && $end == "")
            {
                $where['dq_time'] = ['>=',$start];
            }
            if(isset($end) && $end != "" && isset($start) && $start == "")
            {
                $where['dq_time'] = ['<=',$end];
            }
            if(isset($start) && $start != "" && isset($end) && $end != "")
            {
                $where['dq_time'] = ['between',[$start,$end]];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $field = 'dq.*,cpd_name,cp_name,c_name';
            $field .= ',u1.user_username sendname,u1.user_phone sendphone,u2.user_username getname,u2.user_phone getphone';
            $count = Db::table('up_company_pd_question')->alias('dq')
                ->join('up_u_user u1','dq.dq_send_uid = u1.user_id','left')
                ->join('up_u_user u2','dq.dq_get_uid = u2.user_id','left')
                ->join('up_company_product_data cpd','dq.dq_cpd_id = cpd.cpd_id','left')
                ->join('up_company_product cp','cpd.cpd_cp_id = cp.cp_id','left')
                ->join('up_company c','cp.cp_c_id = c.c_id','left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_company_pd_question')->alias('dq')
                ->join('up_u_user u1','dq.dq_send_uid = u1.user_id','left')
                ->join('up_u_user u2','dq.dq_get_uid = u2.user_id','left')
                ->join('up_company_product_data cpd','dq.dq_cpd_id = cpd.cpd_id','left')
                ->join('up_company_product cp','cpd.cpd_cp_id = cp.cp_id','left')
                ->join('up_company c','cp.cp_c_id = c.c_id','left')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('dq_time desc')
                ->field($field)
                ->select();
            if($lists){
                foreach ($lists as $key => $vo){
                    if($vo['dq_imgs']){
                        $lists[$key]['dq_imgs'] = explode(',',$vo['dq_imgs']);
                    }
//                负责人信息
                    $lists[$key]['userinfo'] = $vo['getname'] . '(' . $vo['getphone'] . ')';
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 查看回复
     * @return mixed|\think\response\Json
     */
    public function seeReplys(){
        $dq_id = input('dq_id');
        $field = 'qr.*,u1.user_id uid1,u1.user_username name1,u1.user_image image1,u1.user_type,u1.user_phone phone1';
        $field .= ',u2.user_username name2';
        $reply = Db::table('up_company_pd_question_reply qr')
            ->join('up_u_user u1','qr.dqr_user_id=u1.user_id','LEFT')
            ->join('up_u_user u2','qr.dqr_reply_user_id=u2.user_id','LEFT')
            ->where('dqr_dq_id',$dq_id)
            ->where('dqr_show',1)
            ->order('dqr_time desc')
            ->field($field)
            ->select();
        foreach ($reply as $k2 => $v2){
            if($v2['dqr_imgs']){
                $reply[$k2]['dqr_imgs'] = explode(',',$v2['dqr_imgs']);
            }else{
                $reply[$k2]['dqr_imgs'] = [];
            }
        }
        $this->assign([
            'reply' => $reply
        ]);
        return $this->fetch('question/reply');
    }
    /**
     * 如果转给了同事，查看转让同事信息
     */
    public function zhuanUsers(){
        if(request()->isAjax()){
            extract(input());
            $where = [
                'user_id' => ['in',trim($uids,',')]
            ];
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $field = 'user_id,user_username,user_sex,user_image,user_phone';
            $count = Db::table('up_u_user')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_u_user')
                ->where($where)
                ->page($NowPage,$limits)
                ->field($field)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $uids = input('uids');
        $this->assign([
            'uids' => trim($uids,',')
        ]);
        return $this->fetch('question/users');
    }
    /**
     * 导出Excel
     * @return \think\response\Json
     */
    public function outExcel(){
        extract(input());
        set_time_limit(0);//防止超时
        $where = [
            'cpd_id' => ['>',0]
        ];
        if(session("company_id") > 0){
            $where['c_id'] = session("company_id");
        }
        if(isset($user_name) && $user_name != ''){
            $where['u1.user_username|u1.user_phone|cpd_name|cp_name|c_name'] = ['like', '%' . $user_name . '%'];
        }
        if(isset($dq_answer) && $dq_answer != ''){
            $where['dq_answer'] = $dq_answer;
        }
        if(isset($dq_zhuan) && $dq_zhuan != ''){
            $where['dq_zhuan'] = $dq_zhuan;
        }
        //开始时间查询
        if(isset($start) && $start != "" && isset($end) && $end == "")
        {
            $where['dqr_time'] = ['>=',$start];
        }
        if(isset($end) && $end != "" && isset($start) && $start == "")
        {
            $where['dqr_time'] = ['<=',$end];
        }
        if(isset($start) && $start != "" && isset($end) && $end != "")
        {
            $where['dqr_time'] = ['between',[$start,$end]];
        }
        if($ids){
            if($ids != 'all'){
                $where['dq_id'] = ['in',$ids];
            }
        }else{
            return json(['code'=>0,'msg'=>'请选择数据！']);
        }
        $field = [
            'dq_id','u1.user_username sendname','u1.user_phone sendphone','dq_content', 'dq_imgs',
            'cpd_name','cp_name','c_name','dq_answer','dq_zhuan',
            'dq_zhuan_uid','u2.user_username getname','u2.user_phone getphone','dq_id reply'
        ];
        $data =  Db::table('up_company_pd_question')->alias('dq')
            ->join('up_u_user u1','dq.dq_send_uid = u1.user_id','left')
            ->join('up_u_user u2','dq.dq_get_uid = u2.user_id','left')
            ->join('up_company_product_data cpd','dq.dq_cpd_id = cpd.cpd_id','left')
            ->join('up_company_product cp','cpd.cpd_cp_id = cp.cp_id','left')
            ->join('up_company c','cp.cp_c_id = c.c_id','left')
            ->where($where)
            ->order('dq_time desc')
            ->field($field)
            ->select();
        if($data){
            foreach ($data as $key => $vo){
                //1、转让信息
                if($vo['dq_zhuan'] == 2){
                    $uids = trim($vo['dq_zhuan_uid'],',');
                    $names = Db::table('up_u_user')
                        ->where('user_id','in',$uids)
                        ->column('concat(user_username,"(",user_phone,")") names');
                    $data[$key]['dq_zhuan_uid'] = implode($names,',');
                }
                //2、回复信息
                $replys = Db::table('up_company_pd_question_reply dqr')
                    ->join('up_u_user u1','dqr.dqr_user_id=u1.user_id','LEFT')
                    ->where('dqr_dq_id',$vo['dq_id'])
                    ->where('dqr_show',1)
                    ->column('concat("(",user_username,"，",user_phone,"，",dqr_time,"，",dqr_content,")") replys');
                if($replys){
                    $data[$key]['reply'] = implode($replys,',');
                }else{
                    $data[$key]['reply'] = '';
                }

            }
            $cellname = [
                '提问ID', '用户姓名', '用户电话', '提问内容', '提问图片',
                '资料名称', '产品名称', '企业名称', '是否回复', '是否转让',
                '转让信息', '负责人名字', '负责人电话', '回复内容(姓名,手机号,回复时间,回复内容)'
            ];
            $status = ['dq_answer','dq_zhuan'];
            $values = [
                'dq_answer' => [1 => '未回复',2 => '已回复'],
                'dq_zhuan' => [1 => '未转让',2 => '已转让']
            ];
            $res = exportExcel('questiondata',time(),$cellname,$data,$status,$values);
            return json($res);
        }else{
            return json(['code'=>0,'msg'=>'此条件下无数据']);
        }
    }
}